<?php
session_start();
if($_SESSION['user']=="") {
    ?>
    <script>
        alert('当前未登录！');
        window.location.href = '../login.html';
    </script>
    <?php
}
//连接数据库
try {
    function success(){
        $result=[
            'error' => 0,
            'message' => '成功'
        ];
        return json_encode($result);
    }
    function error($mag){
        $data = [
            'error' => 1,
            'message' => $mag
        ];
        return json_encode($data);
    }
    
    //连接数据库
    
    $db = new PDO('mysql:dbname=liaotian;host=106.14.7.147','root','123456');
    $update= $db->prepare('insert into roomRecord (roomId,username,content,time) VALUES(:roomId,:username,:content,:time)');
    $data=[
        ':roomId' => $_POST['roomId'],
        ':username' => $_POST['username'],
        ':content' => $_POST['content'],
        ':time' => time()
    ];
    if($update->execute($data) === false){
        print_r($update->errorInfo());
    }
    $re=$update->fetch();
    if(empty($_POST['content'])){
        die(error('内容不能为空'));
    };
    echo success();
} catch (PDOException $e) {
    echo 'Connection failed';
}
